package com.lwm.killer.service;

import com.lwm.killer.dao.UserDao;
import com.lwm.killer.domain.User;
import com.lwm.killer.exception.GlobalException;
import com.lwm.killer.redis.RedisService;
import com.lwm.killer.redis.prefix.UserTokenKey;
import com.lwm.killer.result.CodeMsg;
import com.lwm.killer.util.MD5Util;
import com.lwm.killer.util.UUIDUtil;
import com.lwm.killer.vo.LoginVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@Service
public class UserService {

    @Autowired
    UserDao userDao ;

    @Autowired
    RedisService redisService;

    public   static  final  String  COOKI_NAME_TOKEN = "token";

    public User getById(Long id ){
        return  userDao.getById(id);
    }


    public CodeMsg login(HttpServletResponse response ,LoginVo loginVo){
        if(loginVo==null){
//            return CodeMsg.SERVER_ERROR;
            throw new GlobalException(CodeMsg.SERVER_ERROR);
        }
        String mobile = loginVo.getMobile().trim();
        String pass = loginVo.getPassword().trim();
        User user = getById(Long.parseLong(mobile)) ;
        if(user == null){
//            return CodeMsg.MOBILE_NOT_EXIST;
            throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
        }


        /*
        *验证密码
         */
        String dbPass = user.getPassword();
        String dbSalt = user.getSalt();
        String calPass = MD5Util.formPassToDBPass(pass,dbSalt);
        if(!calPass.equals(dbPass)){
//            return CodeMsg.PASSWORD_ERROR;
            throw  new GlobalException(CodeMsg.PASSWORD_ERROR);
        }
        String token = UUIDUtil.uuid();
        addCookie(response,token,user);
        return CodeMsg.SUCCESS;
    }


    /*
    *登陆成功 向Cookie中写入Token
     */
    private void addCookie(HttpServletResponse response, String token, User user) {
        redisService.set(UserTokenKey.token, token, user);
        Cookie cookie = new Cookie(COOKI_NAME_TOKEN, token);
        cookie.setMaxAge(UserTokenKey.token.expireSeconds());
        cookie.setPath("/");
        response.addCookie(cookie);
    }


    /*
    *通过Token获取登陆人员的操作信息
     */
    public User getByToken(HttpServletResponse response,String token) {
        if(StringUtils.isEmpty(token)){
            return  null;
        }
        User user = redisService.get(UserTokenKey.token,token,User.class);
        if(user!=null){
            redisService.expireForSeconds(UserTokenKey.token,token,60*30);
        }
        return  user;
    }





//    @Transactional
//    public boolean testTx(){
//
//        User  user = new User();
//        user.setId(2);
//        user.setName("gaopufa");
//        userDao.insertUser(user);
//        User  user2 = new User();
//        user2.setId(3);
//        user2.setName("gaopufa");
//        userDao.insertUser(user2);
//        return true;
//    }



}
